<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class frontend extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('mdata');
		$this->sesina = unserialize(base64_decode($this->session->userdata('ses_data')));
		$this->captcha = $this->session->userdata('captcha');
		$this->load->library('pagination');
	}
	
	public function index()
	{
		/*$this->load->library('user_agent');
			xxx
		if ($this->agent->is_browser())
		{
			$agent = $this->agent->browser().' '.$this->agent->version();
		}
		elseif ($this->agent->is_robot())
		{
			$agent = $this->agent->robot();
		}
		elseif ($this->agent->is_mobile())
		{
			$agent = $this->agent->mobile();
		}
		else
		{
			$agent = 'Unidentified User Agent';
		}

		echo $agent;

		echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.) 
		exit;
		*/
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){
			$data=$this->sesina;
		}
		
		
		
		//else{$data="";}
		$data["produk"]=$this->mdata->get_data("tbl_produk_user","filter_top");
		//$data["allproduct"]=$this->mdata->get_data("allproduct");
		//print_r ($data['allproduct']);
		//exit;
		show('frontend/home',$data);
	}
	
	public function portofolio($p1="",$p2="",$p3="",$p4="",$p5=""){
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){
			$data=$this->sesina;
		}
		$config = array();
			$jml = $jml = $this->db->get('tbl_porto_folio');
			//print_r ($jml);
			$config["base_url"] = base_url() . "frontend/portofolio/paging";
			$config['total_rows'] = $jml->num_rows();
			$config["per_page"] = 21;
			$config['first_page'] = 'Awal';
			$config['last_page'] = 'Akhir';
			$config['next_page'] = '&laquo;';
			$config['prev_page'] = '&raquo;';
			$config['uri_segment'] = 4;
			$this->pagination->initialize($config);
				$data["portofolio"]=$this->mdata->get_data("portofolio","paging",$p2,$config["per_page"]);
				$data["halaman"] = $this->pagination->create_links();
				//$data['query'] = $this->m_karyawan->ambil_karyawan($config['per_page'], $id);
				$data["jml"]=count($data["portofolio"]);
		
		
		
		
		//$data["portofolio"]=$this->mdata->get_data("portofolio");
		show2('frontend/portofolio',$data);
	}
	
	public function faq(){
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){
			$data=$this->sesina;
		}
		$data["faq"]=$this->mdata->get_data("faq");
		show2('frontend/faq',$data);
	}
	
	public function agen($p1="",$p2="",$p3="",$p4="",$p5=""){
	//echo $p2;
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){
			$data=$this->sesina;
		}
			$config = array();
			$jml = $jml = $this->db->get('tbl_toko_user');
			//print_r ($jml);
			$config["base_url"] = base_url() . "frontend/agen/paging";
			$config['total_rows'] = $jml->num_rows();
			$config["per_page"] = 4;
			$config['first_page'] = 'Awal';
			$config['last_page'] = 'Akhir';
			$config['next_page'] = '&laquo;';
			$config['prev_page'] = '&raquo;';
			$config['uri_segment'] = 4;
			$this->pagination->initialize($config);
				$data["agen"]=$this->mdata->get_data("agen","paging",$p2,$config["per_page"]);
				$data["halaman"] = $this->pagination->create_links();
				//$data['query'] = $this->m_karyawan->ambil_karyawan($config['per_page'], $id);
				$data["jml"]=count($data["agen"]);
				//echo $data["jml"];
		
		
		
		//$data["agen"]=$this->mdata->get_data("agen");
		//$data["count_product"]=$this->mdata->get_data("count_product");
		show2('frontend/agen',$data);
	}
	
	public function input_pertanyaan($p1=""){
		/*$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		$data["pertanyaan"]=$this->mdata->input_pertanyaan();
		$this->load->view('frontend/alert_pertanyaan');
		*/
		foreach($_POST as $k=>$v) $post[$k] = $this->input->post($k);
		echo $this->mdata->simpan($p1,$post);
		//show2('frontend/agen',$data);
	}
	
	public function narsis($p1="",$p2="",$p3="",$p4="",$p5=""){
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){
			$data=$this->sesina;
		}
			$config = array();
			$jml = $jml = $this->db->get('tbl_narsis');
			//print_r ($jml);
			$config["base_url"] = base_url() . "frontend/narsis/paging";
			$config['total_rows'] = $jml->num_rows();
			$config["per_page"] = 21;
			$config['first_page'] = 'Awal';
			$config['last_page'] = 'Akhir';
			$config['next_page'] = '&laquo;';
			$config['prev_page'] = '&raquo;';
			$config['uri_segment'] = 4;
			$this->pagination->initialize($config);
				$data["narsis"]=$this->mdata->get_data("narsis","paging",$p2,$config["per_page"]);
				$data["halaman"] = $this->pagination->create_links();
				//$data['query'] = $this->m_karyawan->ambil_karyawan($config['per_page'], $id);
				$data["jml"]=count($data["narsis"]);
		
		
		
		
		//$data["narsis"]=$this->mdata->get_data("narsis");
		show2('frontend/narsis',$data);
	}
	
	public function product($p1='',$p2='')
	{
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){
			$data=$this->sesina;
		}
		
		$data["produk"]=$this->mdata->get_data("highlight_product","$p1");
		$data["url"]=base_url()."frontend/product/".$p1;
		
		show2('frontend/product',$data);
	}
	
	public function product_agen($p1='',$p2='')
	{
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){
			$data=$this->sesina;
		}
		$data["produk_agen"]=$this->mdata->get_data("product_agent","$p1");
		show2('frontend/product',$data);
	}
	
	function get_data($p1="",$p2=""){
		echo $this->mdata->get_data($p1,$p2); 
		//test
		
	}
	
	public function paging($limit,$url,$total,$result)
	{
		$config = array();
        $config["base_url"] = $url;
        $config["total_rows"] = $total;
        $config["per_page"] = $limit;
        $config["uri_segment"] = 4;

        $this->pagination->initialize($config);

        $page = ($limit) ? $limit : 0;
        $data["all"] = $result;
        $data["links"] = $this->pagination->create_links();
		return $data;
	}
	
	function get_form($p1="",$p2="",$p3="",$p4="",$p5=""){
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){
			$data=$this->sesina;
		}
		switch($p1){
			case "daftar_produk":
			$config = array();
			$jml = $jml = $this->db->get('tbl_produk_user');
			//print_r ($jml);
			$config["base_url"] = base_url() . "frontend/get_form/daftar_produk/all/filter_toko";
			$config['total_rows'] = $jml->num_rows();
			$config["per_page"] = 4;
			$config['first_page'] = 'Awal';
			$config['last_page'] = 'Akhir';
			$config['next_page'] = '&laquo;';
			$config['prev_page'] = '&raquo;';
			$config['uri_segment'] = 6;
			$this->pagination->initialize($config);
				$data["produk"]=$this->mdata->get_data("tbl_produk_user",'filter_toko',$p4,$config["per_page"]);
				$data["halaman"] = $this->pagination->create_links();
				//$data['query'] = $this->m_karyawan->ambil_karyawan($config['per_page'], $id);
				$data["jml"]=count($data["produk"]);
				//echo $data["jml"];
				if($p2=='all'){return show2('frontend/'.$p1,$data);}	
			break;
		
			/*case "daftar_produk":
				//paging
				
				$config = array();
				$config["base_url"] = base_url() . "frontend/get_form/daftar_produk/all/$p3";
				$config["total_rows"] = $this->mdata->get_data("jml");
				$config["per_page"] = 6;
				$config["uri_segment"] = 4;

				$this->pagination->initialize($config);

				$page = ($p4) ? $p4 : 0;
				
				//$data["all"] = $this->mdata->get_data($p1, $p2,$config["per_page"], $page);//$this->mshop->get_produk($config["per_page"], $page);
				$data["links"] = $this->pagination->create_links();
				
				
				//show1('detail_produk',$data);
				
				//end paging
				
				
				//$data["produk"]=$this->mdata->get_data("tbl_produk_user","get",$p2);
				//$data["jml"]=count($data["produk"]);	
				//if($p2=='all'){return show2('frontend/'.$p1,$data);}	
			break;
			*/
			
			case "tentang":
				$data["produk"]=$this->mdata->get_data("tbl_produk_user","get");
				return show2('frontend/'.$p1,$data);
			break;
			
			case "product_agen":
			/*paging*/
			$config = array();
			$jml = $this->db->query('select * from tbl_produk_user where tbl_toko_user_id = "'.$p2.'"');	
			$config2["base_url"] = base_url() . "frontend/get_form/product_agen/".$p2."/";
			$config2['total_rows'] = $jml->num_rows();
			$config2["per_page"] = 4;
			$config2['first_page'] = 'Awal';
			$config2['last_page'] = 'Akhir';
			$config2['next_page'] = '&laquo;';
			$config2['prev_page'] = '&raquo;';
			$config2['uri_segment'] = 5;
			$this->pagination->initialize($config2);
			/*end paging*/
			//echo $p2;
				$data["produk"]=$this->mdata->get_data("tbl_produk_user","filter_agen",$p2,$p3,$config2["per_page"]);
				$data["halaman"] = $this->pagination->create_links();
				//$data['query'] = $this->m_karyawan->ambil_karyawan($config['per_page'], $id);
				$data["jml"]=count($data["produk"]);
				
				return show2('frontend/daftar_produk',$data);
			break;
			
			case "daftar":
				$sts=$this->input->post('sts');
				$mod=$this->input->post('mod');
				if($mod==1){return $this->load->view("frontend/form_daftar");}
				else {
					$data["produk"]=$this->mdata->get_data("highlight_product",$this->input->post('data'));
					//print_r($this->input->post('data'));
					return $this->load->view("frontend/kirim",$data);
				}
			break;
			
			case "own":
				$sts=$this->input->post('sts');
				$mod=$this->input->post('mod');
				if($mod==1){return $this->load->view("frontend/form_daftar");}
				else {
					$data["produk"]=$this->mdata->get_data("highlight_product",$this->input->post('data'));
					//print_r($this->input->post('data'));
					return $this->load->view("frontend/kirim2",$data);
				}
			break;
			
			case "pemesanan1":
			case "pemesanan2":
			case "pemesanan3":
				return show2('frontend/'.$p1,$data);
			break;
			
			case "dtg":
			case "konveksi":
			case "harga":
				//$data["produk"]=$this->mdata->get_data("tbl_produk_user","get");
				return show2('frontend/'.$p1,$data);
			break;
			case "label":
				$data["toko"]=$this->mdata->get_data("tbl_toko_user");
				return show2('frontend/'.$p1,$data);
			break;
			case "warna":
				$data["warna"]=$this->mdata->get_data("cl_warna");
				return show2('frontend/'.$p1,$data);
			break;
			
			
			
			
			default : return $this->load->view("frontend/form_daftar");
		}
		
		//show2('frontend/'.$p1,$data);
		$this->load->view("frontend/".$p1,$data);
		//test
		
	}
	
	function get_combo($p1,$valfilter="",$val="",$val2=""){
		$optTemp =""; 
		switch($p1){
				case 'cl_warna':
					$rec=$this->mdata->isi_combo('cl_warna','id','warna');
				break;
				
				case 'cl_kategori':
					$rec=$this->mdata->isi_combo('cl_kategori','id','kategori');
				break;
		}
		
		$optTemp .= "<option value=''>------ Pilih ------</option>";			
			foreach($rec as $v=>$k)
			{
				if($this->input->post("v") == $k['id'])
					$optTemp .= "<option selected value='".$k['id']."'>".$k['text']."</option>";
				else 
					$optTemp .= "<option value='".$k['id']."'>".$k['text']."</option>";
			}
			echo $optTemp;	
		
	}
	
	function buatcapcha($rand){
		$RandomStr = md5(microtime());// md5 to generate the random string
		$ResultStr = substr($RandomStr,0,5);//trim 5 digit
		$NewImage = imagecreatefromjpeg("assets/images/back_captcha.jpg");//image create by existing image and as back ground
		$font='application/libraries/ROCCB.ttf';

		$TextColor = imagecolorallocate($NewImage, 1, 34, 128);//text color-white

		imagettftext($NewImage, 22, 4, 25, 25, $TextColor, $font, $ResultStr);

		$this->session->set_userdata("captcha", $ResultStr);
		
		header("Content-type: image/jpeg");// out out the image
		imagejpeg($NewImage);//Output image to browser/**/
	}
	
	function checkCaptcha($txt){
		if($txt==$this->captcha){
			echo "sama";
		} else {
			echo "tidak sama";
		}	
	}
	
	function cek_user(){
		$user=$this->input->post("user_id");
		$res=$this->db->get_where("tbl_user",array("user_id"=>$user))->result_array();
		if(count($res)>0){echo "ada";}else{echo "ok";}
	}
	
}
